"""Customized menu visualization.
"""
from django import template
from treemenus.models import Menu, MenuItem
from treemenus import menuapp

register = template.Library()

@register.inclusion_tag('main/menu.html', takes_context=True)
def show_menu(context, menu_name, menu_type=None):
    menu = Menu.objects.get(name=menu_name)
    context['menu'] = menu

    # currently unused
    # if menu_type:
    #    context['menu_type'] = menu_type
    
    return context

@register.inclusion_tag('main/menu_item.html', takes_context=True)
def show_menu_item(context, menu_item):
    if not isinstance(menu_item, MenuItem):
        raise template.TemplateSyntaxError(
            'Given argument must be a MenuItem object.')

    path = context['request'].path
    context['menu_item'] = menu_item
    context['menu_item_selected'] = menuapp.is_selected(menu_item, path)

    return context

@register.inclusion_tag('main/breadcrumb.html', takes_context=True)
def show_breadcrumb(context, menu_name):
    path = context['request'].path
    context['menu_path'] = menuapp.get_breadcrumb(menu_name, path)
    return context

